/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.generation.service.impl;

import com.mcx.exception.EntityNotFoundException;
import com.mcx.modules.generation.domain.Account;
import com.mcx.modules.generation.service.AccountService;
import com.mcx.modules.generation.service.dto.AccountQueryCriteria;
import com.mcx.modules.generation.service.mapstruct.AccountMapper;
import lombok.RequiredArgsConstructor;
import com.mcx.modules.generation.repository.AccountRepository;
import com.mcx.modules.generation.service.dto.AccountDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.mcx.utils.PageUtil;
import com.mcx.utils.QueryHelp;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author mcx
* @date 2022-03-17
**/
@Service
@RequiredArgsConstructor
public class AccountServiceImpl implements AccountService {

    private final AccountRepository accountRepository;
    private final AccountMapper accountMapper;

    @Override
    public Map<String,Object> queryAll(AccountQueryCriteria criteria, Pageable pageable){
        Page<Account> page = accountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(accountMapper::toDto));
    }

    @Override
    public List<AccountDto> queryAll(AccountQueryCriteria criteria){
        return accountMapper.toDto(accountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AccountDto findById(Integer id) {
        Account account = accountRepository.findById(id).orElseGet(Account::new);
        return accountMapper.toDto(account);
    }

    @Override
    public int countByUsernameAndPassword(String username, String password) {
        return accountRepository.countByUsernameAndPassword(username, password);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountDto create(Account resources) {
        return accountMapper.toDto(accountRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Account resources) {
        Account account = accountRepository.findById(resources.getId()).orElseGet(Account::new);
        account.copy(resources);
        accountRepository.save(account);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            accountRepository.deleteById(id);
        }
    }

    @Override
    public AccountDto findByName(String userName) {
        Account user = accountRepository.findByUsername(userName);
        if (user == null) {
            throw new EntityNotFoundException(Account.class, "name", userName);
        } else {
            return accountMapper.toDto(user);
        }
    }

    @Override
    public AccountDto findByPhone(String phone) {
        Account user = accountRepository.findByPhone(phone);
        return accountMapper.toDto(user);
    }

}